﻿#include "commconfig.h"
#include <QFileInfo>


CommConfig::CommConfig()
{
    copyResourceToFile(":/source/config/app_config.ini","./config/app_config.ini");
    _set=new QSettings("./config/app_config.ini",QSettings::IniFormat);
}

CommConfig::~CommConfig()
{
    _set->deleteLater();
}

CommConfig &CommConfig::instance()
{
    static CommConfig _instance;
    return _instance;
}

TcpCfg CommConfig::getTcpCfg(QString groupName)
{
    TcpCfg param;
    if(!_set->contains(QString("%1/address").arg(groupName)))
    {
        TcpCfg setParam;
        setParam.address="127.0.0.1:502";
        setParam.timeout=3000;
        setTcpCfg(groupName,setParam);
    }
    _set->beginGroup(groupName);
    param.address=_set->value("address").toString();
    param.timeout=_set->value("timeout").toInt();
    _set->endGroup();
    return param;
}

void CommConfig::setTcpCfg(QString groupName, const TcpCfg &cfg)
{
    QSettings *set=_set;
    set->beginGroup(groupName);
    set->setValue("address",cfg.address);
    set->setValue("timeout",cfg.timeout);
    set->endGroup();
    set->sync();
}

SerialPortParam CommConfig::getSerialPortCfg(QString groupName)
{
    SerialPortParam param;
    if(!_set->contains(QString("%1/com_name").arg(groupName)))
    {
        SerialPortParam setParam;
        setSerialPortCfg(groupName,setParam);
    }
    _set->beginGroup(groupName);
    param.comName=_set->value("com_name").toString();
    param.baudRate=_set->value("baud_rate").toString();
    param.dataBits=_set->value("data_bits").toString();
    param.stopBits=_set->value("stop_bits").toString();
    param.parity=_set->value("parity").toString();
    param.flowControl=_set->value("flow_control").toString();
    _set->endGroup();
    return param;
}

void CommConfig::setSerialPortCfg(QString groupName, const SerialPortParam &cfg)
{
    QSettings *set=_set;
    set->beginGroup(groupName);
    set->setValue("com_name",cfg.comName);
    set->setValue("baud_rate",cfg.baudRate);
    set->setValue("data_bits",cfg.dataBits);
    set->setValue("stop_bits",cfg.stopBits);
    set->setValue("parity",cfg.parity);
    set->setValue("flow_control",cfg.flowControl);
    set->endGroup();
    set->sync();
}

RequestCfg CommConfig::getReqCfg(QString groupName)
{
    if(!_set->contains(QString("%1/is_save").arg(groupName)))
    {
        RequestCfg setParam;
        setParam.isSave=true;
        setParam.isHex=true;
        setParam.curCmd="ff ff ff";
        setReqCfg(groupName,setParam);
    }
    RequestCfg param;
    _set->beginGroup(groupName);
    param.isSave=_set->value("is_save").toBool();
    param.isHex=_set->value("is_hex").toBool();
    param.curCmd=_set->value("cur_cmd").toString();
    _set->endGroup();
    return param;
}

void CommConfig::setReqCfg(QString groupName, const RequestCfg &cfg)
{
    qDebug()<<__FUNCTION__<<__LINE__<<groupName;
    QSettings *set=_set;
    set->beginGroup(groupName);
    set->setValue("is_save",cfg.isSave);
    set->setValue("is_hex",cfg.isHex);
    set->setValue("cur_cmd",cfg.curCmd);
    set->endGroup();
    set->sync();
}


bool CommConfig::copyResourceToFile(const QString &resourcePath, const QString &destinationPath)
{
    // 检查目标文件是否已经存在
    QFileInfo checkFile(destinationPath);
    if (checkFile.exists() && checkFile.isFile()) {
        qDebug() << "File already exists:" << destinationPath;
        return false;  // 文件已存在，不进行复制
    }

    // 打开资源文件 (以只读方式)
    QFile resourceFile(resourcePath);
    if (!resourceFile.open(QIODevice::ReadOnly)) {
        qWarning() << "Cannot open resource file:" << resourcePath;
        return false;
    }

    // 创建目标文件
    QFile destinationFile(destinationPath);

    // 检查目标文件夹是否存在，不存在则创建
    if (!destinationFile.open(QIODevice::WriteOnly)) {
        qWarning() << "Cannot open destination file for writing:" << destinationPath;
        return false;
    }

    // 读取资源文件的内容并写入目标文件
    destinationFile.write(resourceFile.readAll());

    // 关闭文件
    resourceFile.close();
    destinationFile.close();

    return true;
}
